#include <cassert>

#include "matrix.h"

namespace ads {
// O(colmax * rowmax)
int* Matrix::snake(int **a, int colmax, int rowmax) {
  assert(a && colmax > 0 && rowmax > 0);
  int *result = new int[colmax * rowmax];
  int index = 0;
  int row = 0, col = 0;
  bool normal = true;
  while (true) {
    if (row >= rowmax) break;
    
    result[index++] = a[row][col];
    if (normal) {
      ++col;
      if (col >= colmax) {
        normal = !normal;
        col--;
        row++;
      }
    } else {
      --col;
      if (col < 0) {
        normal = !normal;
        col++;
        row++;
      }
    }
  }
  return result;
}
}  // end of namespace
